﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace AMOOF {
    public class ToggleButton : Panel {

        private bool active;

        public string ActiveImage { set; get; }
        public string MouseOverImage { set; get; }
        public string ClickedImage { set; get; }
        public string InactiveImage { set; get; }
        public int Index { set; get; }
        public string Text { set; get; }
        public Bitmap ForegroundImage { set; get; }
        public Font Font { set; get; }
        public bool Active {
            set {
                active = value;
                UpdateBackground();
            }
            get { return active; }
        }

        public bool MouseIn { set; get; }

        public ToggleButton() {
            InitializeComponents();
        }

        private void InitializeComponents() {
            Width = 200;
            Height = 54;
            this.Font = new Font("Arial", 11, FontStyle.Regular);
            this.BackgroundImageLayout = ImageLayout.Stretch;

            ActiveImage = "toggle_active_200x54";
            MouseOverImage = "toggle_hover_200x54";
            ClickedImage = "toggle_clicked_200x54";
            InactiveImage = "toggle_inactive_200x54";

            Active = false;
            MouseIn = false;
            this.MouseDown += new MouseEventHandler(ToggleButton_MouseDown);
            this.MouseUp += new MouseEventHandler(ToggleButton_MouseUp);
            this.MouseEnter += new EventHandler(ToggleButton_MouseEnter);
            this.MouseLeave += new EventHandler(ToggleButton_MouseLeave);
        }

        protected override void OnPaint(PaintEventArgs e) {
            
            base.OnPaint(e);
            //Text = "Optimize Models";
            //NavFont = new Font("Arial", 13, FontStyle.Bold);
            SizeF size = new SizeF();
            size = e.Graphics.MeasureString(Text, Font);
            float x = (this.Width - size.Width) / 2;
            float y = ((this.Height - size.Height) / 2);
            Brush b = this.Enabled ? Brushes.White : Brushes.Gray;
            if (ForegroundImage != null) {
                e.Graphics.DrawImage(ForegroundImage, (this.Width - ForegroundImage.Width) / 2 , 2, 17, 27);
            }
            e.Graphics.DrawString(Text, Font, b, x, y, StringFormat.GenericDefault);
        }

        private void UpdateBackground() {
            if (Active) {
                this.BackgroundImage = (Bitmap)Properties.Resources.ResourceManager.GetObject(ActiveImage);
            }
            else {
                this.BackgroundImage = (Bitmap)Properties.Resources.ResourceManager.GetObject(InactiveImage);
            }
        }

        /*
         * Internal component event handlers. They provide no functionality, just image changes
         */
        private void ToggleButton_MouseDown(object sender, EventArgs e) {
            this.BackgroundImage = (Bitmap)Properties.Resources.ResourceManager.GetObject(ClickedImage);
            
        }

        private void ToggleButton_MouseEnter(object sender, EventArgs e) {
            MouseIn = true;
            object o = Reflector.GetResource<Bitmap>(MouseOverImage);
            this.BackgroundImage = (Bitmap)Properties.Resources.ResourceManager.GetObject(MouseOverImage);
        }

        private void ToggleButton_MouseLeave(object sender, EventArgs e) {
            MouseIn = false;
            UpdateBackground();
        }

        private void ToggleButton_MouseUp(object sender, EventArgs e) {
            
            if (MouseIn) {
                if (!Active) {
                    this.BackgroundImage = (Bitmap)Properties.Resources.ResourceManager.GetObject(ActiveImage);
                }
                else {
                    this.BackgroundImage = (Bitmap)Properties.Resources.ResourceManager.GetObject(MouseOverImage);
                }
            }
        }
    }
}
